package com.bean;

import com.annotation.AdultLimit;

/**
 * @author Cc
 * 2024-04-02
 */
public class Student {
    private String name;
    @AdultLimit(18)
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public Student() {
    }

    public Student(String name, int age) throws NoSuchFieldException {
        this.name = name;
        AdultLimit annotation = this.getClass().getDeclaredField("age").getAnnotation(AdultLimit.class);

        int value = annotation.value();

        if (age < value) {
            throw new RuntimeException("FBI Open The Door");
        }
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setAge(int age) throws NoSuchFieldException {
        AdultLimit annotation = this.getClass().getDeclaredField("age").getAnnotation(AdultLimit.class);

        int value = annotation.value();

        if (age < value) {
            throw new RuntimeException("FBI Open The Door");
        }
        this.age = age;
    }
}
